package com.aptemo.webetek.client.widgets.teams;

import java.util.ArrayList;
import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.GotAsynchCallback;
import com.aptemo.webetek.client.commands.impl.DeleteLabelAction;
import com.aptemo.webetek.client.commands.impl.GetLabelsAction;
import com.aptemo.webetek.client.commands.impl.JoinGroupAction;
import com.aptemo.webetek.client.commands.responses.GotLabelsList;
import com.aptemo.webetek.client.commands.responses.ResponseStatus;
import com.aptemo.webetek.client.events.DecisionEvent;
import com.aptemo.webetek.client.events.DecisionHandler;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.events.TeamChangedEvent;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.mvp.BaseDisplay;
import com.aptemo.webetek.client.mvp.BasePresenter;
import com.aptemo.webetek.shared.LabelBean;
import com.aptemo.webetek.shared.TeamBean;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;

public class UserGroupRowPresenter implements BasePresenter
{

	public interface Display extends BaseDisplay
	{
		HasText getNameField();

		HasText getDescriptionField();

		HasClickHandlers getPanelClick();

		String getClickedLink(ClickEvent event);

		void setBetLabels(List<LabelBean> list, String targetPrefix);

		HasDecisionHandlers getJoinDialogConfirmation();

		void hideJoinDialog();

		HasDecisionHandlers getDeleteConfirmation();

		void hideDeleteTagDialog();

		HasText getJoinSubject();

		HasText getJoinNote();

		HasOpenHandlers<DisclosurePanel> getLabelTreeOpenHandler();
	}

	private HandlerManager					meventBus;
	private UserGroupRowPresenter.Display	display;
	private TeamBean						mdata;
	private WebetekUserServiceAsync			rpcService;
	private String							id;

	public UserGroupRowPresenter(HandlerManager eventbus, WebetekUserServiceAsync rpc, UserGroupRowPresenter.Display view, TeamBean data)
	{
		meventBus = eventbus;
		mdata = data;
		rpcService = rpc;
		this.id = data.getId();
		display = view;
	}

	private void bindDisplay()
	{

		display.getNameField().setText(mdata.getName());

		display.getDescriptionField().setText(mdata.getDescription());

		display.getPanelClick().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				meventBus.fireEvent(new TeamChangedEvent(id, ""));
			}
		});

		display.getJoinDialogConfirmation().addDecisionHandler(new DecisionHandler()
		{

			@Override
			public void onConfirmation(DecisionEvent event)
			{
				join(event.getInfoField());
			}
		});

		display.getDeleteConfirmation().addDecisionHandler(new DecisionHandler()
		{

			@Override
			public void onConfirmation(DecisionEvent event)
			{
				deleteTag(event.getInfoField(), event.getId());
			}
		});

		display.getLabelTreeOpenHandler().addOpenHandler(new OpenHandler<DisclosurePanel>()
		{
			
			@Override
			public void onOpen(OpenEvent<DisclosurePanel> event)
			{
				loadLabelsGroups();	
			}
		});
	}

	protected void deleteTag(final HasText text, String tagid)
	{
		DeleteLabelAction action = new DeleteLabelAction(tagid);
		rpcService.invoke(action, new GotAsynchCallback<ResponseStatus>()
		{

			@Override
			public void onSuccess(ResponseStatus status)
			{
				display.hideDeleteTagDialog();
				//loadLabelsGroups();
				meventBus.fireEvent(new TeamChangedEvent(id, ""));
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				text.setText(message);
			}
		});

	}

	private void join(final HasText info)
	{
		String note = display.getJoinNote().getText();
		String subject = display.getJoinSubject().getText();

		JoinGroupAction action = new JoinGroupAction(this.id, note, subject);
		rpcService.invoke(action, new GotAsynchCallback<UserBean>()
		{

			@Override
			public void onSuccess(UserBean result)
			{
				// TODO set general info
				display.hideJoinDialog();
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				info.setText(message);
			}
		});
	}

	protected void loadLabelsGroups()
	{
		GetLabelsAction ac = new GetLabelsAction(id);
		rpcService.invoke(ac, new GotLabelsList()
		{

			@Override
			public void onError(String message)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void got(ArrayList<LabelBean> result)
			{
				display.setBetLabels(result, id);
			}
		});
	}

	@Override
	public void go(HasWidgets container)
	{
		bindDisplay();

		//loadLabelsGroups();
	}

}
